var api = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },
  getUserInfo: function(opt) {
    var a = this;
    wx.login({
      success(res) {
        if (res.code) {
          wx.request({
            url: api.globalData.Api + '/getopenid',
            method: 'POST',
            data: { code: res.code },
            header: api.header,
            success: function(res) {
              if (res.data.status == 0){
                console.log('获取unionid异常');
              } else if (res.data.status == 1) { // 用户已经存在
                wx.setStorageSync('userData', res.data.data);
                wx.reLaunch({
                  url: '/pages/discover/discover'
                });
              } else if (res.data.status == 2) { // 用户不存在
                let openid = res.data.data.openid, unionid = res.data.data.unionid;
                wx.getSystemInfo({
                  success(res) {
                    let version = res.version;
                    wx.getSetting({ // 授权验证
                      success(res) {
                        if (res.authSetting['scope.userInfo']) {
                          wx.showLoading({
                            title: "正在登录",
                            mask: !0
                          });
                          wx.getUserInfo({
                            withCredentials: true,
                            lang: 'zh_CN',
                            success(res) {
                              var userInfo = res.userInfo;
                              wx.request({
                                url: api.globalData.Api + '/regist',
                                method: 'POST',
                                data: {
                                  nickName: userInfo.nickName,
                                  avatarUrl: userInfo.avatarUrl,
                                  country: userInfo.country,
                                  province: userInfo.province,
                                  city: userInfo.city,
                                  gender: userInfo.gender,
                                  openid: openid,
                                  unionid: unionid,
                                  wechatNum: version
                                },
                                header: api.header,
                                success(res) {
                                  wx.setStorageSync('userData', res.data.data);
                                  wx.reLaunch({
                                    url: '/pages/discover/discover'
                                  }), wx.hideLoading();
                                }
                              })
                            }
                          })
                        } else {
                          console.log('拒绝了授权！');
                        }
                      }
                    })
                  }
                })
              }
            }
          })
        } else {
          console.log('登录失败！')
        }
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})